/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.SubjectDAO;
import com.tces.controller.SubjectOfferingDAO;
import com.tces.controller.SubjectScheduleDAO;
import com.tces.model.SubjectOffering;
import com.tces.model.SubjectSchedule;
import com.tces.model.User;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author arnolda
 */
public class SubjectScheduleAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(SubjectScheduleAction.class);
    private List<SubjectSchedule> subjectScheduleList;
    private User user;
    private String id, oper, subjectOfferingFullName, subjectCode, subjectSchedule, instructorFullName, scheduleStatus;

    public String viewStudentSubjectOptions() throws Exception {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
//            log.info("User role:" + user.getRoleName());
//            if (user.getRoleName().equalsIgnoreCase("Administrator")) {
            subjectScheduleList = SubjectScheduleDAO.getSubjectScheduleOption();
//            } else {
//                subjectScheduleList = SubjectScheduleDAO.find(user.getCollegeID());
//            }
            log.info("subjectScheduleList SIZE:" + subjectScheduleList.size());

        } catch (Exception ex) {
            log.error(ex);

        }
        return SUCCESS;
    }

    public String view() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
            log.info("User role:" + user.getRoleName());
            if (user.getRoleName().equalsIgnoreCase("Administrator") || user.getRoleName().equalsIgnoreCase("Guidance Counseling Head")) {
                subjectScheduleList = SubjectScheduleDAO.find();
            } else {
                subjectScheduleList = SubjectScheduleDAO.find(user.getCollegeID());
            }
            log.info("subjectScheduleList SIZE:" + subjectScheduleList.size());

        } catch (Exception ex) {
            log.error(ex);

        }
        return SUCCESS;
    }

    public String edit() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");

            log.info("PARAM :" + id + oper + subjectOfferingFullName + subjectCode + subjectSchedule + instructorFullName + scheduleStatus);
            if (oper.equalsIgnoreCase("add")) {

                if (SubjectScheduleDAO.add(getSubjSchedule()) > 0) {
                    addActionMessage("Successfull inserted.");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("edit")) {

                if (SubjectScheduleDAO.update(getSubjSchedule()) > 0) {
                    addActionMessage("Successfull update.");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("del")) {
                if (SubjectScheduleDAO.delete(Integer.parseInt(id)) > 0) {
                    addActionMessage("Successfull deletion of Subject Offering[ID:" + id + "]");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else {
                addActionMessage("Invalid operation!");
                return ERROR;
            }
        } catch (Exception ex) {
            addActionError(ex.toString());
            log.error(ex);
            return ERROR;
        }
    }

    private SubjectSchedule getSubjSchedule() throws Exception {
        SubjectSchedule subjSchedule = new SubjectSchedule();
        if (subjectOfferingFullName != null) {
            subjSchedule.setSubjectOfferingID(Integer.parseInt(subjectOfferingFullName));
        }
        if (subjectCode != null) {
            subjSchedule.setSubjectID(Integer.parseInt(subjectCode));
            subjSchedule.setSubjectCode(SubjectDAO.find(Integer.parseInt(subjectCode)).getSubjectName());
        }
        if (instructorFullName != null) {
            subjSchedule.setInstructorID(Integer.parseInt(instructorFullName));
        }
        if (scheduleStatus != null) {
            subjSchedule.setScheduleStatus(Integer.parseInt(scheduleStatus));
        }
        if (id != null && !(id.equalsIgnoreCase("_empty"))) {
            log.info("" + id.equalsIgnoreCase("_empty"));
            subjSchedule.setSubjectScheduleID(Integer.parseInt(id));
        }
        subjSchedule.setSubjectSchedule(subjectSchedule);


        return subjSchedule;
    }

    public List<SubjectSchedule> getSubjectScheduleList() {
        return subjectScheduleList;
    }

    public void setSubjectScheduleList(List<SubjectSchedule> subjectScheduleList) {
        this.subjectScheduleList = subjectScheduleList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getInstructorFullName() {
        return instructorFullName;
    }

    public void setInstructorFullName(String instructorFullName) {
        this.instructorFullName = instructorFullName;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public String getScheduleStatus() {
        return scheduleStatus;
    }

    public void setScheduleStatus(String scheduleStatus) {
        this.scheduleStatus = scheduleStatus;
    }

    public String getSubjectCode() {
        return subjectCode;
    }

    public void setSubjectCode(String subjectCode) {
        this.subjectCode = subjectCode;
    }

    public String getSubjectOfferingFullName() {
        return subjectOfferingFullName;
    }

    public void setSubjectOfferingFullName(String subjectOfferingFullName) {
        this.subjectOfferingFullName = subjectOfferingFullName;
    }

    public String getSubjectSchedule() {
        return subjectSchedule;
    }

    public void setSubjectSchedule(String subjectSchedule) {
        this.subjectSchedule = subjectSchedule;
    }
}
